package Printer;

import static com.qb.devtest.MainActivity.mActivity;

import android.content.Context;
import android.graphics.Bitmap;

import com.caysn.autoreplyprint.AutoReplyPrint;
import com.sun.jna.Pointer;
import com.sun.jna.WString;

import java.util.Date;

import utils.MyUtil;

public class CaysnPrinter {

    Pointer h = Pointer.NULL;
    PrinterCMD pcmd = new PrinterCMD();

    public String OpenPort() {
//        Pointer h = Pointer.NULL;
        String[] listUsbPort = AutoReplyPrint.CP_Port_EnumUsb_Helper.EnumUsb();
        if (listUsbPort != null) {
            for (String usbPort : listUsbPort) {
                if (usbPort.contains("0x4B43") || usbPort.contains("0x0FE6")) {
                    h = AutoReplyPrint.INSTANCE.CP_Port_OpenUsb(usbPort, 1);
                    break;
                }
            }
        }
        //Log.i(TAG, h == Pointer.NULL ? "OpenPort Failed" : "OpenPort Success");
        if (h == Pointer.NULL) {
            //showMessageOnUiThread("OpenPort Failed");
        }
        return h == Pointer.NULL?"打印机连接失败":"success";
    }

    public String ClosePort() {
        if (h != Pointer.NULL) {
            AutoReplyPrint.INSTANCE.CP_Port_Close(h);
            h = Pointer.NULL;
        }
        return h == Pointer.NULL?"closed":"端口关闭失败";
    }

    //打印图片
    public void PrintImage(Bitmap bitmap) {
        if (h != Pointer.NULL) {
            try {
//                bitmap = MyUtil.getImageFromAssetsFile(mActivity,"RasterImage/barcode.bmp");
                AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(h, 1);
                AutoReplyPrint.CP_Pos_PrintRasterImageFromData_Helper.PrintRasterImageFromBitmap(h, bitmap.getWidth(), bitmap.getHeight(), bitmap, AutoReplyPrint.CP_ImageBinarizationMethod_Thresholding, AutoReplyPrint.CP_ImageCompressionMethod_None);
                AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(h, 1);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    // 走纸
    public void PageGo(int pagenum) {
        if (h != Pointer.NULL) {
            AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(h, pagenum);
        }
    }

    // 切纸
    public void CutPage(int pagenum) {
        if (h != Pointer.NULL) {
            AutoReplyPrint.INSTANCE.CP_Pos_FeedAndHalfCutPaper(h);
        }
    }

    // 开钱箱
    public void OpenCashBox() {
        if (h != Pointer.NULL) {
            AutoReplyPrint.INSTANCE.CP_Pos_KickOutDrawer(h, 0, 100, 100);
            AutoReplyPrint.INSTANCE.CP_Pos_KickOutDrawer(h, 1, 100, 100);
        }
    }

    // 打印文字
    public void PrintTest(String s,String barcode) {
        int paperWidth = 384;

        AutoReplyPrint.INSTANCE.CP_Pos_ResetPrinter(h);
        AutoReplyPrint.INSTANCE.CP_Pos_SetMultiByteMode(h);
        AutoReplyPrint.INSTANCE.CP_Pos_SetMultiByteEncoding(h, AutoReplyPrint.CP_MultiByteEncoding_UTF8);

        // 打印标题
        /*
         * CP_Pos_SetTextScale(h,i,i1);  i：0正常宽、1倍宽；i1：0正常高、1倍高
         * */
        AutoReplyPrint.INSTANCE.CP_Pos_SetTextScale(h, 0, 1);
        AutoReplyPrint.INSTANCE.CP_Pos_SetAlignment(h, AutoReplyPrint.CP_Pos_Alignment_HCenter);
        AutoReplyPrint.INSTANCE.CP_Pos_PrintText(h, "58小票打印测试\r\n");
        AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(h, 1);

        AutoReplyPrint.INSTANCE.CP_Pos_SetTextScale(h, 0, 0);
        AutoReplyPrint.INSTANCE.CP_Pos_PrintText(h, s);

        // 打印条码
        PrinterBarcode(60,3,barcode);

        /*
        WString str = new WString(s);
        boolean result = AutoReplyPrint.INSTANCE.CP_Pos_PrintTextInUTF8(h, str);
        MyUtil.showLog("caysn print result",result?"success":"打印失败");
        */

        PageGo(2);
    }

    //58小票打印
    public void PrinterTest58() {
        int paperWidth = 384;

        AutoReplyPrint.INSTANCE.CP_Pos_ResetPrinter(h);
        AutoReplyPrint.INSTANCE.CP_Pos_SetMultiByteMode(h);
        AutoReplyPrint.INSTANCE.CP_Pos_SetMultiByteEncoding(h, AutoReplyPrint.CP_MultiByteEncoding_UTF8);

        // 打印标题
        /*
        * CP_Pos_SetTextScale(h,i,i1);  i：0正常宽、1倍宽；i1：0正常高、1倍高
        * */
        AutoReplyPrint.INSTANCE.CP_Pos_SetTextScale(h, 0, 1);
        AutoReplyPrint.INSTANCE.CP_Pos_SetAlignment(h, AutoReplyPrint.CP_Pos_Alignment_HCenter);
        AutoReplyPrint.INSTANCE.CP_Pos_PrintText(h, "58小票打印测试\r\n");
        AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(h, 1);

        // 打印明细
        AutoReplyPrint.INSTANCE.CP_Pos_SetTextScale(h, 0, 0);
        AutoReplyPrint.INSTANCE.CP_Pos_SetAlignment(h, AutoReplyPrint.CP_Pos_Alignment_Right);
        AutoReplyPrint.INSTANCE.CP_Pos_PrintText(h, "TEL 9999-99-9999 \r\n");
        AutoReplyPrint.INSTANCE.CP_Pos_SetAlignment(h, AutoReplyPrint.CP_Pos_Alignment_HCenter);
        AutoReplyPrint.INSTANCE.CP_Pos_PrintText(h, MyUtil.formatDateTime(new Date()));
        AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(h, 1);

        AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(h, 1);
        AutoReplyPrint.INSTANCE.CP_Pos_PrintText(h, "苹果");
        AutoReplyPrint.INSTANCE.CP_Pos_SetHorizontalAbsolutePrintPosition(h, paperWidth - 12 * 6);
        AutoReplyPrint.INSTANCE.CP_Pos_PrintText(h, "40.00");

        AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(h, 1);
        AutoReplyPrint.INSTANCE.CP_Pos_PrintText(h, "香蕉");
        AutoReplyPrint.INSTANCE.CP_Pos_SetHorizontalAbsolutePrintPosition(h, paperWidth - 12 * 6);
        AutoReplyPrint.INSTANCE.CP_Pos_PrintText(h, "60.00");

        AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(h, 1);
        AutoReplyPrint.INSTANCE.CP_Pos_PrintText(h, "大鸭梨");
        AutoReplyPrint.INSTANCE.CP_Pos_SetHorizontalAbsolutePrintPosition(h, paperWidth - 12 * 6);
        AutoReplyPrint.INSTANCE.CP_Pos_PrintText(h, "120.00");


        //打印横线
        String line = "";
        for (int i = 0; i < paperWidth / 12; ++i)
            line += " ";
        AutoReplyPrint.INSTANCE.CP_Pos_SetTextUnderline(h, 1);
        AutoReplyPrint.INSTANCE.CP_Pos_PrintText(h, line);
        AutoReplyPrint.INSTANCE.CP_Pos_SetTextUnderline(h, 0);
        AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(h, 1);

        //打印合计
        AutoReplyPrint.INSTANCE.CP_Pos_SetTextScale(h, 1, 0);
        AutoReplyPrint.INSTANCE.CP_Pos_PrintText(h, "合计");
        AutoReplyPrint.INSTANCE.CP_Pos_SetHorizontalAbsolutePrintPosition(h, paperWidth - 12 * 2 * 7);
        AutoReplyPrint.INSTANCE.CP_Pos_PrintText(h, "220.00");
        AutoReplyPrint.INSTANCE.CP_Pos_SetTextScale(h, 0, 0);
        AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(h, 1);
        AutoReplyPrint.INSTANCE.CP_Pos_PrintText(h, "付款");
        AutoReplyPrint.INSTANCE.CP_Pos_SetHorizontalAbsolutePrintPosition(h, paperWidth - 12 * 7);
        AutoReplyPrint.INSTANCE.CP_Pos_PrintText(h, "250.00");
        AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(h, 1);
        AutoReplyPrint.INSTANCE.CP_Pos_SetTextBold(h, 1);// 加粗
        AutoReplyPrint.INSTANCE.CP_Pos_PrintText(h, "找零");
        AutoReplyPrint.INSTANCE.CP_Pos_SetHorizontalAbsolutePrintPosition(h, paperWidth - 12 * 6);
        AutoReplyPrint.INSTANCE.CP_Pos_PrintText(h, "30.00");
        AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(h, 1);

        // 打印条码及二维码
        PrinterBarcode(60,3,"1234567890123");
//        PrinterQRcode("小票二维码打印测试"+MyUtil.formatDateTime(new Date()));

        AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(h, 1);
        AutoReplyPrint.INSTANCE.CP_Pos_Beep(h, 1, 500);
    }

    // 打印条码
    public void PrinterBarcode(int bh,int bw,String s) {
        AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(h, 1);
        AutoReplyPrint.INSTANCE.CP_Pos_SetBarcodeHeight(h, bh);
        AutoReplyPrint.INSTANCE.CP_Pos_SetBarcodeUnitWidth(h, bw);
        AutoReplyPrint.INSTANCE.CP_Pos_SetBarcodeReadableTextPosition(h, AutoReplyPrint.CP_Pos_BarcodeTextPrintPosition_BelowBarcode);
        // AutoReplyPrint.CP_Pos_BarcodeType_EAN13 ,条码类型
        AutoReplyPrint.INSTANCE.CP_Pos_PrintBarcode(h, AutoReplyPrint.CP_Pos_BarcodeType_ITF, s);
    }
    // 打印二维码
    public void PrinterQRcode(String s) {
        AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(h, 1);
        AutoReplyPrint.INSTANCE.CP_Pos_SetAlignment(h, AutoReplyPrint.CP_Pos_Alignment_HCenter);
        AutoReplyPrint.INSTANCE.CP_Pos_SetBarcodeUnitWidth(h, 8);
        AutoReplyPrint.INSTANCE.CP_Pos_PrintQRCode(h, 0, AutoReplyPrint.CP_QRCodeECC_L, s);
    }
}
